﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace TasksWebService
{
    [WebService(Namespace = "http://mp.tasks.com/2010/", Description = "Tasks Library Web Service")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1, Namespace = "http://mp.tasks.com/2010/")]
    [System.ComponentModel.ToolboxItem(false)]
    public class Tasks : System.Web.Services.WebService, TasksModel.ITasksService
    {
        #region ITasksService Members

        /// <summary>
        /// Get API version
        /// </summary>
        /// <returns>1.0.0.0</returns>
        [WebMethod]
        public string Ping()
        {
            return TasksModel.TaskLibrary.ApiVersion;
        }

        /// <summary>
        /// Get list of libraries
        /// </summary>
        /// <returns>[guid|CreatedBy]</returns>
        [WebMethod]
        public List<string> GetLibraries()
        {
            try
            {
                return StorageHelper.GetLibraries();
            }
            catch (SystemException)
            {
                throw new System.Web.Services.Protocols.SoapException("I/O Error", System.Xml.XmlQualifiedName.Empty);
            }
        }

        /// <summary>
        /// Get xml of the library
        /// </summary>
        /// <param name="guid">guid in valid form</param>
        /// <returns>xml</returns>
        [WebMethod]
        public string GetLibrary(string guid)
        {
            try
            {
                return StorageHelper.Get(new Guid(guid));
            }
            catch (OverflowException)
            {
                throw new System.Web.Services.Protocols.SoapException("Invalid GUID format", System.Xml.XmlQualifiedName.Empty);
            }
            catch (FormatException)
            {
                throw new System.Web.Services.Protocols.SoapException("Invalid GUID format", System.Xml.XmlQualifiedName.Empty);
            }
            catch (SystemException)
            {
                throw new System.Web.Services.Protocols.SoapException("I/O Error", System.Xml.XmlQualifiedName.Empty);
            }
        }

        /// <summary>
        /// Persist library, merging with existing if already exists.
        /// </summary>
        /// <param name="xml">xml of the library</param>
        /// <returns>new xml of the resulting library</returns>
        [WebMethod]
        public string SaveLibrary(string xml)
        {
            try
            {
                return StorageHelper.SaveAndMerge(xml);
            }
            catch (FormatException)
            {
                throw new System.Web.Services.Protocols.SoapException("Invalid xml format passed", System.Xml.XmlQualifiedName.Empty);
            }
            catch (InvalidOperationException)
            {
                throw new System.Web.Services.Protocols.SoapException("Serialization Error", System.Xml.XmlQualifiedName.Empty);
            }
            catch (SystemException)
            {
                throw new System.Web.Services.Protocols.SoapException("I/O Error", System.Xml.XmlQualifiedName.Empty);
            }
        }
        #endregion
    }
}